/**
 * UserServiceImpl.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.service.impl.backend.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.site.dao.backend.system.UsersDao;
import com.site.dto.backend.system.AuthorityBizBean;
import com.site.dto.backend.system.ResourseBizBean;
import com.site.dto.backend.system.RoleBizBean;
import com.site.dto.backend.system.UsersBizBean;
import com.site.model.backend.system.AuthorityInfo;
import com.site.model.backend.system.ResourseInfo;
import com.site.model.backend.system.RoleInfo;
import com.site.model.backend.system.UsersInfo;
import com.site.service.backend.system.UsersService;
import com.site.util.PropertyUtils;
import com.site.util.StringUtils;
import com.site.util.base.service.BaseService;
import com.site.util.base.service.BaseServiceImpl;
import com.site.util.constant.Constants;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 
 * @author  
 */
@Component("usersService")
public class UsersServiceImpl extends BaseServiceImpl implements UsersService{
	
	private Log log = LogFactory.getLog(UsersServiceImpl.class);
	
	private UsersDao usersDao;

	@SuppressWarnings("all")
	@Override
	public Object doExecute(Object obj, String command) throws SystemException, ApplicationException {
		
		if(BaseService.COMMAND_SAVE.equals(command)){
			save((UsersBizBean)obj);
		} else if(BaseService.COMMAND_DELETE.equals(command)){
			return delete((List<UsersBizBean>)obj);
		} else if(BaseService.COMMAND_UPDATE.equals(command)){
			update((UsersBizBean)obj);
		} else if("disabled".equals(command)){
			// 锁定
			disabled((String)obj);
		} else if("enabled".equals(command)){
			// 解锁
			enabled((String)obj);
		}
		return null;
	}
	/**
	 * 解锁
	 * @param obj
	 */
	private void enabled(String obj) {
		if(!StringUtils.isEmpty(obj)){
			List<Long> ids = StringUtils.splitToListByLong(obj);
			for (Long long1 : ids) {
				UsersInfo po = usersDao.getEntityById(UsersInfo.class, long1);
				if(po != null){
					po.setStatus(Constants.COMM_N);
					po.setModifiedDate(new Date());
					usersDao.update(po);
				}
			}
		}
	}
	/**
	 *  锁定
	 * @param obj
	 */
	private void disabled(String obj) {
		if(!StringUtils.isEmpty(obj)){
			List<Long> ids = StringUtils.splitToListByLong(obj);
			for (Long long1 : ids) {
				UsersInfo po = usersDao.getEntityById(UsersInfo.class, long1);
				if(po != null){
					po.setStatus(Constants.COMM_Y);
					po.setModifiedDate(new Date());
					usersDao.update(po);
				}
			}
		} else {
			log.info("用户字符串为空。。。");
		}
	}

	private void update(UsersBizBean obj) {
		if(null != obj){
			UsersInfo po = usersDao.getEntityById(UsersInfo.class, obj.getUserId());
			if(po != null){
				PropertyUtils.copyNotNullProperties(po, obj);
				usersDao.update(po);
			}
		}
	}

	private String delete(List<UsersBizBean> obj) {
		StringBuffer sb = new StringBuffer();
		for (UsersBizBean usersBizBean : obj) {
			UsersInfo po = null;
			try {
				if(null != usersBizBean && usersBizBean.getUserId() != null){
					po = (UsersInfo)usersDao.getEntityById(UsersInfo.class, usersBizBean.getUserId());
					usersDao.delete(UsersInfo.class,usersBizBean.getUserId());
					if(null != po){
						sb.append(po.getRealName());
						sb.append(";");
					}
				}
			} catch (Exception e) {
				log.info(e.getMessage());
				if(null != po){
					sb.append(po.getRealName());
					sb.append(";");
				}
			}
		}
		
		if(!StringUtils.isEmpty(sb.toString())){
			log.debug(sb.toString());
			return StringUtils.removeLastComma2(sb.toString());
		}
		
		return null;
	}
	
	private void save(UsersBizBean obj) {
		if(null != obj){
			UsersInfo po = new UsersInfo();
			PropertyUtils.copyProperties(po, obj);
			Date saveTime = new Date();
			po.setCreatedDate(saveTime);	
			usersDao.save(po);
		} else {
			log.info("系统用户保存失败！");
		}
	}

	@Override
	public List<UsersBizBean> getUserList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UsersBizBean> getUserList(QueryPage queryPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UsersBizBean> getNotDeptUserList(Integer deptId,
			UsersBizBean userBizBean, QueryPage queryPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UsersBizBean> getNotRoleUserList(Long roleId,UsersBizBean vo, QueryPage queryPage) {
		List<UsersBizBean> usersBizBeanList = null;
		List<UsersInfo> usersInfoList = usersDao.getNotRoleUserList(roleId, vo, queryPage);
		if(null != usersInfoList && usersInfoList.size() > 0){
			UsersBizBean usersBizBean = null;
			usersBizBeanList = new ArrayList<UsersBizBean>();
			for (UsersInfo usersInfo : usersInfoList) {
				if(null != usersInfo){
					usersBizBean = new UsersBizBean();
					PropertyUtils.copyProperties(usersBizBean, usersInfo);
					usersBizBeanList.add(usersBizBean);
				}
			}
		}
		return usersBizBeanList;
	}

	@Override
	public UsersBizBean getUserByName(String userName) {
		UsersInfo po = usersDao.getUserByName(userName);
		UsersBizBean vo = new UsersBizBean();
		if (po == null) {
			return null;
		}
		PropertyUtils.copyProperties(vo, po);
		return vo;
	}

	@Override
	public List<UsersBizBean> getUserList(UsersBizBean userBizBean, QueryPage queryPage) {
		List<UsersBizBean> usersBizBeanList = new ArrayList<UsersBizBean>();
		List<UsersInfo> usersInfoList = new ArrayList<UsersInfo>();
		usersInfoList = usersDao.getUserList(userBizBean, queryPage);
		if(null != usersInfoList && usersInfoList.size() > 0){
			for (UsersInfo usersInfo : usersInfoList) {
				if(null != usersInfo){
					UsersBizBean vo = new UsersBizBean();
					PropertyUtils.copyProperties(vo, usersInfo);
					usersBizBeanList.add(vo);
				}
			}
		}
		return usersBizBeanList;
	}

	@Override
	public UsersBizBean getUserById(Long userId) {
		UsersInfo po = (UsersInfo)usersDao.getEntityById(UsersInfo.class ,userId);
		UsersBizBean vo = new UsersBizBean();
		if(null != po){
			PropertyUtils.copyProperties(vo, po);
		}
		return vo;
	}

	@Override
	public void addLoginCount(UsersBizBean user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public UsersBizBean checkTheSameUserStart(Integer userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UsersBizBean getUserByNameLower(String userName) {
		UsersInfo po = usersDao.getUserByNameLower(userName);
		UsersBizBean vo = new UsersBizBean();
		if (po == null) {
			return null;
		}
		try {
			PropertyUtils.copyProperties(vo, po);
			return vo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<UsersBizBean> getUserDepartMent(Integer userId,
			QueryPage queryPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UsersBizBean> getUserRole(Integer userId, QueryPage queryPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UsersBizBean getUserByIdSql(Integer userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UsersBizBean getUserBySql(UsersBizBean userBizBean) {
		// TODO Auto-generated method stub
		return null;
	}

	public UsersDao getUsersDao() {
		return usersDao;
	}
	@Resource
	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	@Override
	public UsersBizBean loadUserByUsername(String userName) {
		if(!StringUtils.isEmpty(userName)){
			UsersInfo usersInfo = usersDao.getUserByName(userName);
			if(null != usersInfo){
				UsersBizBean vo = new UsersBizBean();
				PropertyUtils.copyProperties(vo, usersInfo);
				Set<RoleBizBean> roleBizBeanSet = new HashSet<RoleBizBean>();
				if(null != usersInfo && usersInfo.getRoleSet().size() > 0){
					for (RoleInfo  roleInfo: usersInfo.getRoleSet()) {
						if(null != roleInfo){
							RoleBizBean roleBizBean = new RoleBizBean();
							PropertyUtils.copyProperties(roleBizBean, roleInfo);
							// 角色资源
							Set<ResourseBizBean> resourseBizBeanSet = null;
							if(null != roleInfo && roleInfo.getResourseSet().size() > 0){
								resourseBizBeanSet = new HashSet<ResourseBizBean>();
								for (ResourseInfo resourseInfo : roleInfo.getResourseSet()) {
									if(null != resourseInfo){
										ResourseBizBean resourseBizBean = new ResourseBizBean();
										PropertyUtils.copyProperties(resourseBizBean, resourseInfo);
										resourseBizBeanSet.add(resourseBizBean);
									}
								}
							}
							// 角色权限
							Set<AuthorityBizBean> authorityBizBeanSet = null;
							if(null != roleInfo && roleInfo.getAuthoritySet().size() > 0){
								authorityBizBeanSet = new HashSet<AuthorityBizBean>();
								for (AuthorityInfo authorityInfo : roleInfo.getAuthoritySet()) {
									if(null != authorityInfo){
										AuthorityBizBean authorityBizBean = new AuthorityBizBean();
										PropertyUtils.copyProperties(authorityBizBean, authorityInfo);
										authorityBizBeanSet.add(authorityBizBean);
									}
								}
							}
							// 设置角色资源和权限
							if(null != resourseBizBeanSet && resourseBizBeanSet.size() >0 && null != authorityBizBeanSet && authorityBizBeanSet.size() > 0){
								roleBizBean.setResourseBizBeanSet(resourseBizBeanSet);
								roleBizBean.setAuthoritySet(authorityBizBeanSet);
							}
							roleBizBeanSet.add(roleBizBean);
						}
					}
					vo.setRoleSet(roleBizBeanSet);
				}
				return vo;
			}
		}
		return null;
	}
}
